//
//  MainViewController.swift
//  XMGWB
//
//  Created by xiaomage on 15/12/1.
//  Copyright © 2015年 xiaomage. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //tabBar.tintColor = UIColor.orangeColor()
        
        // 添加子控制器
       // addChildViewControllers()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBar.addSubview(composeBtn)
        let rect = composeBtn.frame
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        composeBtn.frame = CGRect(x: 2 * width, y: 0, width: width, height: rect.height)
        
    }
    /// 添加所有子控制器
//    func addChildViewControllers()
//    {
//        //load json file
//        //load view controllers dynamicly
//        guard let filePath = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil) else{
//            HYLog("cannnt obtain json file")
//            return
//        }
//        guard let data: NSData = NSData(contentsOfFile: filePath) else{
//            HYLog("cannot obtain data from pathfile")
//            return
//        }
//        
//        do{
//            
//            let obj = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as! [[String: AnyObject]]
//            
//            for dict in obj{
//                let title = dict["title"] as? String
//                let vcName = dict["vcName"] as? String
//                let imageName = dict["imageName"] as? String
//                addChildViewController(vcName, title: title, imageName: imageName)
//            }
//            
//        }catch{
//            addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
//            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
//            addChildViewController("NullTableViewController", title: "", imageName: "")
//            addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
//            addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
//            
//        }
//        
//    }


//    func addChildViewController(childController: String?, title: String?, imageName: String?) {
//        
//        guard let name = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else{
//            
//            HYLog("获取命名空间失败")
//            return
//        }
//        var cls: AnyClass? = nil
//        if let  vcName = childController{
//            cls = NSClassFromString(name + "." + vcName)
//        }
//        
//        guard let clsType = cls as? UITableViewController.Type else{
//            HYLog("无法转换类型")
//            return
//        }
//        let childController = clsType.init()
//        
//        
// 
//        
//        
//        // 1.2设置自控制的相关属性
////        childController.navigationItem.title = title
////        childController.tabBarItem.title = title
//        HYLog(childController)
//        // 该方法会由内向外的设置标题
//        childController.title = title
//        if let imName = imageName{
//            childController.tabBarItem.image = UIImage(named: imName)
//            childController.tabBarItem.selectedImage = UIImage(named: imName + "_highlighted")
//        }
//        
//
//        
//        
//        // 1.3包装一个导航控制器
//        let nav = UINavigationController(rootViewController: childController)
//        // 1.4将子控制器添加到UITabBarController中
//        addChildViewController(nav)
//    }
    @objc private func composeBtnDidClick(btn: UIButton){
        HYLog(btn)
    }
    
    private lazy var composeBtn: UIButton = {
        () -> UIButton
        in
        let btn = UIButton(imageName: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
      /*  btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)*/
        
        btn.addTarget(self, action: Selector("composeBtnDidClick:"), forControlEvents: .TouchUpInside)
        
        //btn.sizeToFit()
        return btn
    }()
    

}
